using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VVGPOS
    {
        public DataRow Convert2VGPOS(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryVGPOS(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["VGPMANDANT"] = " ".PadRight(2 ); 
         dwReturn["VGPAUFKRS"] = " ".PadRight(2 ); 
         dwReturn["VGPAUFLFD"] = " ".PadRight(6 ); 
         dwReturn["VGPAUFPOS"] = " ".PadRight(3 ); 
         dwReturn["VGPLFDNR"] = " ".PadRight(0 ); 
         dwReturn["VGPAENZL"] = " ".PadRight(0 ); 
         dwReturn["VGPAENB"] = " ".PadRight(8 ); 
         dwReturn["VGPAEND"] = " ".PadRight(0 ); 
         dwReturn["VGPNEUB"] = " ".PadRight(8 ); 
         dwReturn["VGPNEUD"] = " ".PadRight(0 ); 
         dwReturn["VGPVAUFKRS"] = " ".PadRight(2 ); 
         dwReturn["VGPVAUFLFD"] = " ".PadRight(6 ); 
         dwReturn["VGPVAUFPOS"] = " ".PadRight(3 ); 
         dwReturn["VGPMENGE"] = " ".PadRight(0 ); 
         dwReturn["VGPFILLER"] = " ".PadRight(44 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapVGPOS(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveVGPOS(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryVGPOS(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapVGPOS(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertVGPOS(dwLocal, ITXDBVERSION);
            }
            UpdateVGPOS(dwLocal);
        }
        public void InsertVGPOS(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO VGPOS(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryVGPOS(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM VGPOS WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM VGPOS  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "VGPOS";
            Return = Result;
            return Return;
        }
        public void DeleteVGPOS(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM VGPOS   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateVGPOS(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE VGPOS SET "
                    +"VGPMANDANT= :VGPMANDANT,"
                    +"VGPAUFKRS= :VGPAUFKRS,"
                    +"VGPAUFLFD= :VGPAUFLFD,"
                    +"VGPAUFPOS= :VGPAUFPOS,"
                    +"VGPLFDNR= :VGPLFDNR,"
                    +"VGPAENZL= :VGPAENZL,"
                    +"VGPAENB= :VGPAENB,"
                    +"VGPAEND= :VGPAEND,"
                    +"VGPNEUB= :VGPNEUB,"
                    +"VGPNEUD= :VGPNEUD,"
                    +"VGPVAUFKRS= :VGPVAUFKRS,"
                    +"VGPVAUFLFD= :VGPVAUFLFD,"
                    +"VGPVAUFPOS= :VGPVAUFPOS,"
                    +"VGPMENGE= :VGPMENGE,"
                    +"VGPFILLER= :VGPFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("VGPMANDANT", OracleType.NVarChar).Value = dwData["VGPMANDANT"].ToString(); 
          or1.Parameters.Add("VGPAUFKRS", OracleType.NVarChar).Value = dwData["VGPAUFKRS"].ToString(); 
          or1.Parameters.Add("VGPAUFLFD", OracleType.NVarChar).Value = dwData["VGPAUFLFD"].ToString(); 
          or1.Parameters.Add("VGPAUFPOS", OracleType.NVarChar).Value = dwData["VGPAUFPOS"].ToString(); 
          or1.Parameters.Add("VGPLFDNR", OracleType.NVarChar).Value = dwData["VGPLFDNR"].ToString(); 
          or1.Parameters.Add("VGPAENZL", OracleType.NVarChar).Value = dwData["VGPAENZL"].ToString(); 
          or1.Parameters.Add("VGPAENB", OracleType.NVarChar).Value = dwData["VGPAENB"].ToString(); 
          or1.Parameters.Add("VGPAEND", OracleType.NVarChar).Value = dwData["VGPAEND"].ToString(); 
          or1.Parameters.Add("VGPNEUB", OracleType.NVarChar).Value = dwData["VGPNEUB"].ToString(); 
          or1.Parameters.Add("VGPNEUD", OracleType.NVarChar).Value = dwData["VGPNEUD"].ToString(); 
          or1.Parameters.Add("VGPVAUFKRS", OracleType.NVarChar).Value = dwData["VGPVAUFKRS"].ToString(); 
          or1.Parameters.Add("VGPVAUFLFD", OracleType.NVarChar).Value = dwData["VGPVAUFLFD"].ToString(); 
          or1.Parameters.Add("VGPVAUFPOS", OracleType.NVarChar).Value = dwData["VGPVAUFPOS"].ToString(); 
          or1.Parameters.Add("VGPMENGE", OracleType.NVarChar).Value = dwData["VGPMENGE"].ToString(); 
          or1.Parameters.Add("VGPFILLER", OracleType.NVarChar).Value = dwData["VGPFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
